package org.skyeye.modeling.customerized.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.skyeye.modeling.customerized.model.BaseSwitch;
import org.skyeye.modeling.customerized.model.SimpleOutput;

public class SPDTSwitchFigure extends BaseSwitchFigure {

	private static final Dimension SIZE = new Dimension(50, 50);
	final int R = 2;
	final int MARGIN = 5;
	private Label label;
	/**
	 * Constructor for AndGateFigure.
	 */
	public SPDTSwitchFigure(String ID) {
		setBackgroundColor(LogicColorConstants.spdtSwitch);
		FixedConnectionAnchor rightAnchor = new FixedConnectionAnchor(this);
		rightAnchor.topDown = false;
		rightAnchor.offsetH = 7;
		inputConnectionAnchors.addElement(rightAnchor);
		outputConnectionAnchors.addElement(rightAnchor);
		connectionAnchors.put(BaseSwitch.TERMINAL_RIGHT, rightAnchor);

		FixedConnectionAnchor rightTopAnchor = new FixedConnectionAnchor(this);
		rightTopAnchor.topDown = false;
		rightTopAnchor.offsetH = 7;
		outputConnectionAnchors.addElement(rightTopAnchor);
		inputConnectionAnchors.addElement(rightTopAnchor);
		connectionAnchors.put(BaseSwitch.TERMINAL_RIGHT_TOP, rightTopAnchor);

		this.label = new Label();
		label.setText("S"+ID);
		label.setTextAlignment(Label.LEFT);
		label.setIconTextGap(5);
		this.add(label);
	}

	/**
	 * @see org.eclipse.draw2d.Figure#getPreferredSize(int, int)
	 */
	public Dimension getPreferredSize(int wHint, int hHint) {
		return SIZE;
	}

	/**
	 * @see org.eclipse.draw2d.Figure#paintFigure(Graphics)
	 */
	protected void paintFigure(Graphics g) {
		test(g);
	}

	private void test(Graphics graphics) {
		final float lineInset = Math.max(1.0f, 1) / 2.0f;
		final int inset1 = (int) Math.floor(lineInset);
		final int inset2 = (int) Math.ceil(lineInset);

		final Rectangle r = Rectangle.SINGLETON.setBounds(getBounds());
		r.x += inset1;
		r.y += inset1;
		r.width -= inset1 + inset2;
		r.height -= inset1 + inset2;

		Rectangle leftPoint = new Rectangle();
		leftPoint.x = r.x + MARGIN;
		leftPoint.y = r.y + r.height - 2 * R;
		leftPoint.width = 2 * R;
		leftPoint.height = 2 * R;

		graphics.drawArc(leftPoint, (int) 0, (int) 360);

		Rectangle rightPoint = new Rectangle();
		rightPoint.x = r.x + r.width - 2 * R - MARGIN;
		rightPoint.y = r.y + r.height - 2 * R;
		rightPoint.width = 2 * R;
		rightPoint.height = 2 * R;

		graphics.drawArc(rightPoint, (int) 0, (int) 360);

		Rectangle rightTopPoint = new Rectangle();
		rightTopPoint.x = r.x + r.width - 2 * R - MARGIN;
		rightTopPoint.y = r.y + r.height / 2;
		rightTopPoint.width = 2 * R;
		rightTopPoint.height = 2 * R;
		graphics.drawArc(rightTopPoint, (int) 0, (int) 360);

		graphics.setLineWidthFloat(1);
		graphics.setForegroundColor(ColorConstants.black);

		if (value == 1) {
			graphics.drawLine(new Point(leftPoint.x + 2 * R, leftPoint.y + R),
					new Point(rightPoint.x, rightPoint.y + R));
		} else {
			graphics.drawLine(new Point(leftPoint.x + 2 * R, leftPoint.y + R),
					new Point(rightTopPoint.x, rightTopPoint.y + rightTopPoint.height / 2));

		}

		Point leftConnectionPoint = new Point(r.x, r.y + r.height - R);
		Point rightConnectionPoint = new Point(r.x + r.width, r.y + r.height - R);
		Point rightTopConnectionPoint = new Point(r.x + r.width, r.y + r.height / 2 + R);

		graphics.drawLine(leftConnectionPoint, new Point(leftPoint.x, leftPoint.y + R));
		graphics.drawLine(rightConnectionPoint, new Point(rightPoint.x + 2 * R, rightPoint.y + R));

		graphics.drawLine(rightTopConnectionPoint, new Point(rightTopPoint.x + 2 * R, rightTopPoint.y + R));

		((FixedConnectionAnchor) this.connectionAnchors.get(BaseSwitch.TERMINAL_RIGHT)).offsetH = getBounds().width;
		((FixedConnectionAnchor) this.connectionAnchors.get(BaseSwitch.TERMINAL_RIGHT)).offsetV = R;

		((FixedConnectionAnchor) this.connectionAnchors.get(BaseSwitch.TERMINAL_RIGHT_TOP)).offsetH = getBounds().width;
		((FixedConnectionAnchor) this.connectionAnchors.get(BaseSwitch.TERMINAL_RIGHT_TOP)).offsetV = getBounds().height
				/ 2 - R;

		((FixedConnectionAnchor) this.connectionAnchors.get(BaseSwitch.TERMINAL_LEFT)).offsetV = getBounds().height - R;
		

		Rectangle tmp = new Rectangle(bounds.x + 2,bounds.y  ,bounds.width,10);
		this.label.setBounds(tmp);
	}

}